package pl.mobilebackup.comm.mdb;

import javax.ejb.CreateException;
import javax.ejb.EJBException;
import javax.ejb.MessageDrivenBean;
import javax.ejb.MessageDrivenContext;
import javax.jms.BytesMessage;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;
import javax.management.MBeanServerConnection;
import javax.management.MBeanServerInvocationHandler;
import javax.management.MalformedObjectNameException;
import javax.management.ObjectName;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.resource.spi.work.Work;
import javax.resource.spi.work.WorkException;
import javax.rmi.PortableRemoteObject;

import org.jboss.resource.work.JBossWorkManager;
import org.jboss.resource.work.JBossWorkManagerMBean;

import pl.mobilebackup.comm.exceptions.MessageNotValidException;
import pl.mobilebackup.comm.session.interfaces.ActiveMQTransmitterLocal;
import pl.mobilebackup.comm.session.interfaces.ActiveMQTransmitterLocalHome;

public class ActiveMQReceiver implements MessageDrivenBean, MessageListener {

	private static final long serialVersionUID = 1L;

	protected MessageDrivenContext context = null;
	
	private ActiveMQTransmitterLocalHome transmitterLocalHome;

	private JBossWorkManager wm;
	
	public void ejbCreate() {
		System.out.println("ActiveMQReceiver.ejbCreate()");
		InitialContext context;
		try {
			context = new InitialContext();
			Object ejb = context.lookup("java:comp/env/Transmitter");
			transmitterLocalHome = (ActiveMQTransmitterLocalHome) PortableRemoteObject.narrow(ejb, ActiveMQTransmitterLocalHome.class);
			
			MBeanServerConnection mConnection = (MBeanServerConnection) context.lookup("jmx/invoker/RMIAdaptor");
			ObjectName objectName = new ObjectName("jboss.jca:service=WorkManager");
			JBossWorkManagerMBean jwm = (JBossWorkManagerMBean) MBeanServerInvocationHandler.newProxyInstance(mConnection, objectName, JBossWorkManagerMBean.class, false);
			wm = (JBossWorkManager) jwm.getInstance();
		} catch (NamingException e) {
			e.printStackTrace();
		} catch (MalformedObjectNameException e) {
			e.printStackTrace();
		} catch (NullPointerException e) {
			e.printStackTrace();
		}
	}
	
	public void ejbRemove() throws EJBException {
		System.out.println("ActiveMQReceiver.ejbRemove()");
	}

	public void setMessageDrivenContext(MessageDrivenContext context) throws EJBException {
		this.context = context;
	}

	public void onMessage(Message message) {
		System.out.println("ActiveMQReceiver.onMessage()");
		if(message instanceof TextMessage) {
			System.out.println("->TextMessage");
			TextMessage textMessage = (TextMessage) message;
			try {
				final String messageString = textMessage.getText();
				System.out.println("->Message: " + messageString);
				final ActiveMQTransmitterLocal transmitter = transmitterLocalHome.create();
				wm.doWork(new Work() {

					public void release() {
						System.out.println("ActiveMQReceiver.onMessage.Work.release()");
					}

					public void run() {
						System.out.println("ActiveMQReceiver.onMessage.Work.run()");
						try {
							transmitter.routeMessage(messageString);
						} catch (MessageNotValidException e) {
							e.printStackTrace();
						}
					}
					
				});
			} catch (JMSException e) {
				e.printStackTrace();
			} catch (CreateException e) {
				e.printStackTrace();
			} catch (WorkException e) {
				e.printStackTrace();
			}
		} else if(message instanceof BytesMessage) {
			System.out.println("->FileMessage");
			final BytesMessage fileMessage = (BytesMessage) message;
			try {
				System.out.println("->file length: " + fileMessage.getBodyLength());
				final ActiveMQTransmitterLocal transmitter = transmitterLocalHome.create();
				wm.doWork(new Work() {

					public void release() {
						System.out.println("ActiveMQReceiver.onMessage.Work.release()");
					}

					public void run() {
//						try {
							System.out.println("ActiveMQReceiver.onMessage.Work.run()");
//							FileMessage file = new FileMessage();
//							file.setPath(fileMessage.getStringProperty("path"));
//							file.setUser(fileMessage.getStringProperty("user"));
//							byte[] fileBytes = new byte[(int) fileMessage.getBodyLength()];
//							fileMessage.readBytes(fileBytes);
//							file.setInputFile(fileBytes);
//							transmitter.routeFile(file);
//						} catch (JMSException e) {
//							e.printStackTrace();
//						}
					}
					
				});
			} catch (WorkException e) {
				e.printStackTrace();
			} catch (CreateException e) {
				e.printStackTrace();
			} catch (JMSException e) {
				e.printStackTrace();
			}
			
		}
	}

}
